package com.example.transit.utils;

import android.app.Activity;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimeUtil {
    //获取当前时间戳
    public static String getCurrentTimeMillis() {
        long timeStamp = System.currentTimeMillis();
        return timeStamp + "";
    }

    public static String getTime(Date date, String str) {//可根据需要自行截取数据显示
//        Log.e("ssss", "choice date millis: " + date.getTime());
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat(str);

        return format.format(date);
    }

    //比较两个时间的大小
    public static boolean compareTime(String time1, String time2, String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        Date bt = null;
        Date et = null;
        try {
            bt = sdf.parse(time1);
            et = sdf.parse(time2);
            if (bt.before(et)) {
                //表示bt小于et
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static int differentDays(String time1, String time2, String str) {
        SimpleDateFormat sdf = new SimpleDateFormat(str);

        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(time1);
            date2 = sdf.parse(time2);
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            int day1 = cal1.get(Calendar.DAY_OF_YEAR);
            int day2 = cal2.get(Calendar.DAY_OF_YEAR);

            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);
            if (year1 != year2)   //同一年
            {
                int timeDistance = 0;
                for (int i = year1; i < year2; i++) {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                    {
                        timeDistance += 366;
                    } else    //不是闰年
                    {
                        timeDistance += 365;
                    }
                }

                return timeDistance + (day2 - day1);
            } else    //不同年
            {
                System.out.println("判断day2 - day1 : " + (day2 - day1));
                return day2 - day1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }

    //倒计时
    public static int time = 0;
    public static Timer timer;
    public static TimerTask task;

    public static void CountDownTime(final Activity context, final TextView tv) {
        time = 60;
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        time--;
                        tv.setText("已发送(" + String.valueOf(time) + ")");
                        tv.setEnabled(false);
                        if (time <= 0) {
                            tv.setEnabled(true);
                            tv.setText("重新获取验证码");
                        }
                    }
                });
            }
        };
        timer.schedule(task, time, 1000);
    }

    public static void CountDownClear() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null) {
            task.cancel();
            task = null;
        }
    }
}
